package com.ilock.service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.ilock.bean.Ilockloc;
import com.ilock.bean.IlocklocDAO;
import com.ilock.bean.IuserDAO;
import com.ilock.bean.IlockinfoDAO;

public class LockLocServiceImpl implements ILockLocService {
	
	private IlocklocDAO IlocklocDAO;
	private IlockinfoDAO IlockinfoDAO;
	private IuserDAO IuserDAO;
	private Map<String,Object> lockmap = new HashMap<String,Object>();

	public IlocklocDAO getIlocklocDAO() {
		return IlocklocDAO;
	}
	
	public void setIlocklocDAO(IlocklocDAO ilocklocDAO) {
		IlocklocDAO = ilocklocDAO;
	}

	public IuserDAO getIuserDAO() {
		return IuserDAO;
	}

	public void setIuserDAO(IuserDAO iuserDAO) {
		IuserDAO = iuserDAO;
	}

	public IlockinfoDAO getIlockinfoDAO() {
		return IlockinfoDAO;
	}

	public void setIlockinfoDAO(IlockinfoDAO ilockinfoDAO) {
		IlockinfoDAO = ilockinfoDAO;
	}

	public Map<String, Object> getLockmap() {
		return lockmap;
	}

	public void setLockmap(Map<String, Object> lockmap) {
		this.lockmap = lockmap;
	}

	/**
	 * find lock's location by lock's id
	 * @param lockId
	 * @return
	 */
	public Ilockloc getLockLocById(int lockId){
		Ilockloc ilockloc = null;
		ilockloc =IlocklocDAO.getLockLatestLocation(lockId);
		return ilockloc;
	}
	
	/**
	 * add loc's location into database
	 * @param lockId
	 * @param loclng
	 * 
	 * @param loclat
	 */
	public void addLockLoc(int lockId,double loclng,double loclat){
		Ilockloc tempilockloc = new Ilockloc();
		tempilockloc.setIlockInfoNumber(lockId);
		tempilockloc.setIlockLocLng(loclng);
		tempilockloc.setIlockLocLat(loclat);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		tempilockloc.setIlockLocTime(Timestamp.valueOf((df.format(new Date()).toString())));
		System.out.println(tempilockloc.getIlockLocTime());
		try{
			IlocklocDAO.save(tempilockloc);  
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	
	@Test
	public void getNearbyLocks(){
		IlocklocDAO tempIlocklocDAO = new IlocklocDAO();
		List templist = tempIlocklocDAO.testHql(1, 400.0);
		if(!templist.isEmpty()){
			System.out.println(templist);
		}
	}

	@Override
	public List getLocklocListByTime(int lockid, Timestamp timestamp) {
		// TODO Auto-generated method stub
		return IlocklocDAO.getLocationListbyTime(lockid, timestamp);
	}

	@Override
	public Ilockloc getLockLatestLocation(int lockID) {
		// TODO Auto-generated method stub
		return IlocklocDAO.getLockLatestLocation(lockID);
	}

	@Override
	public List getNearLockList(int lockID,double lng, double lat, double scope) {
		// TODO Auto-generated method stub
		return IlocklocDAO.getNearLockList(lockID, lat, lng, scope);
	}

	@Override
	public Map getNearUserListByToken(String lockToken) {
		// TODO Auto-generated method stub
//		IlockInfo ilockinfo = IlockinfoDAO.
		return null;
	}
	
	@Override
	public List getLoseConnLockList() {
		// TODO Auto-generated method stub
		return IlocklocDAO.getLoseConnLockList();
	}
}
